//
//  Jade
//
//  Created by wangkuncheng on 16/11/30.
//  Copyright © 2016年 KC. All rights reserved.
//

#import "KCKit.h"
#import <WebKit/WebKit.h>
#import "KCTreasureDetailVC.h"
#import "SVProgressHUD.h"
#import "WXApi.h"

@interface KCTreasureDetailVC ()<WKUIDelegate,WKNavigationDelegate>

@property (nonatomic,strong)WKWebView *web;

@end

@implementation KCTreasureDetailVC

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTopicMess];
}
- (void)dealloc {
    KCLog(@"♻️ Dealloc %@", NSStringFromClass([self class]));
}

#pragma mark - requestData

- (void)createTopicMess {
    
    WKUserScript *script = [[WKUserScript alloc] initWithSource:@"" injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    [config.userContentController addUserScript:script];

    _web = [[WKWebView alloc]initWithFrame:CGRectMake(0,20, SCREEN_WIDTH, SCREEN_HEIGHT - 20) configuration:config];
    _web.UIDelegate = self;
    _web.navigationDelegate = self;
    [self.view addSubview:_web];
    NSString *urlStr = [NSString stringWithFormat:@"http://www.icangyu.com/app/treasure.html?item_id=%@",_IDString];
    if (_artID) {
        urlStr = [NSString stringWithFormat:@"http://cert.icangyu.cn/app/article_a.html?item_id=%@",_artID];
    }else {
        UIButton *btnw = [UIButton buttonWithType:UIButtonTypeCustom];
        [btnw setTitle:@"咨询 icangyu5" forState:UIControlStateNormal];
        [btnw setTitleColor:WHITE_COLOR forState:UIControlStateNormal];
        btnw.titleLabel.font = SYSTEM_BOLD_FONT(15);
        btnw.backgroundColor = [UIColor grayColor];
        btnw.titleEdgeInsets = UIEdgeInsetsMake(-25, 0, 0, 0);
        btnw.frame = CGRectMake(0,SCREEN_HEIGHT - 90,SCREEN_WIDTH,90);
        [btnw addTarget:self action:@selector(zixunAction)forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btnw];
    }
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]];
    [_web loadRequest:request];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setImage:[UIImage imageNamed:@"videoaBack"] forState:UIControlStateNormal];
    btn.frame = CGRectMake(15,35,45,45);
    [btn addTarget:self action:@selector(btnClick)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

- (void)btnClick {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)zixunAction {
    
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    pasteboard.string = @"icangyu5";
    [ShareApplication openURL:[NSURL URLWithString:@"wechat://"]];
    
//    WXOpenCustomerServiceReq *req = [[WXOpenCustomerServiceReq alloc] init];
//    req.corpid = @"ww294341d304c8bb47";
//    req.url = @"https://work.weixin.qq.com/kfid/kfc959a886d074688d9";
//    [WXApi sendReq:req completion:nil];

}

// 页面开始加载时调用
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation {
    [SVProgressHUD show];
}
// 当内容开始返回时调用
- (void)webView:(WKWebView *)webView didCommitNavigation:(WKNavigation *)navigation {
}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
    KCLog(@"加载完成");
    [SVProgressHUD dismiss];
    //禁止缩放
    NSString *injectionJSString = @"var script = document.createElement('meta');"
    "script.name = 'viewport';"
    "script.content='width=device-width, user-scalable=no';"
    "document.getElementsByTagName('head')[0].appendChild(script);";
    [webView evaluateJavaScript:injectionJSString completionHandler:nil];
    //字体
//    NSString *fileUrl = [[NSBundle mainBundle] pathForResource:@"FZYaSong-M-GBK" ofType:@"ttf"];
//    NSData *fileData = [NSData dataWithContentsOfFile:fileUrl];
//    NSString *boldFont = [fileData base64EncodedStringWithOptions:0];
//    NSMutableString *javascript = [NSMutableString string];
//    [javascript appendString:[NSString stringWithFormat:@"\
//                                  var boldcss = '@font-face { font-family: \"%@\"; src: url(data:font/ttf;base64,%@) format(\"truetype\");} *{font-family: \"%@\" !important;}'; \
//                                  var head = document.getElementsByTagName('head')[0], \
//                                  style = document.createElement('style'); \
//                                  style.type = 'text/css'; \
//                                  style.innerHTML = boldcss; \
//                                  head.appendChild(style);",@"FZYaSongS-M-GB",boldFont,@"FZYaSongS-M-GB"]];
//     [webView evaluateJavaScript:javascript completionHandler:nil];
    
}

@end
